<?php
/**
 * Filter base class
 */
abstract class Q_Filter_Abstract implements Q_Filter_Interface {

    /**
     * The parameters that were passed to this rule
     */
    protected $_params = array(
        'field' => 0, // default is zero because this allows $rule->validate('some text')
    );
    /**
     * Data we are validating
     */
    protected $_data = array();
    /**
     * Constructor sets up params
     */
    public function __construct($params = array()) {
    
        $this->_init();
        $this->setParams($params);
    
    }
    /**
     * this is for child rules to implement init functionality
     */
    protected function _init() {
    
        // do stuff
    
    }
    /**
     * set up params
     */
    public function setParams($params) {
    
        foreach ($params as $key => $param) {
            $this->_params[$key] = $param;
        }
    
    }
    public function getParam($param) {
    
        if (isset($this->_params[$param])) return $this->_params[$param];
        return false;
    
    }
    /**
     * Call this method to detect if data is valid - this gives isValid() the context of $this->_data
     * which it may use to determine validity of one field based on other fields
     */
    public function filter($data) {
    
        if (!is_array($data)) $data = array($data);
        $this->_data = $data;
        if (isset($this->_data[$this->getParam('field')])) {
            $this->_data[$this->getParam('field')] = $this->doFilter($this->_data[$this->getParam('field')]);
        }
        return $this->_data;
    
    }
    public function setField($field) {
    
        $this->_params['field'] = (string) $field;
    
    }
    public function setParam($key, $val) {
    
        $this->_params[$key] = $val;
    
    }
    /**
     * This is where the filtering is actually delegated to. Indivudual filters should extend this method to
     * provide their filter logic.
     */
    abstract public function doFilter($value);

}